// app.js
import { httpServer } from './utils/httpServer';
App({
  async onLaunch(e) {
    this.setNavBarInfo();

    Promise.prototype.finally = function (callback) {
      let P = this.constructor;
      return this.then(value => {
        P.resolve(callback()).then(() => value);
      }, reason => {
        P.resolve(callback()).then(() => {
          throw reason
        });
      });
    };

    wx.getStorage({
      key: 'openid',
      success: res => {
        httpServer('POST', 'User', 'GetInfo', {
          openid: res.data
        })
        .then(res => {
          if (res.result == 'success') {
            let info = res.info;
            Object.assign(info, res.info, {isLogin: true});
            this.globalData.userInfo = info;
          }
        })
        .catch(err => {
          console.error(err);
          });
      },
      fail: err => {
        console.error('err: ', err);
      }
    })
  },
  globalData: {
    navBarHeight: 0, // 导航栏高度
    menuBottom: 0, // 胶囊距底部边距
    menuRight: 0, // 胶囊距右侧边距
    menuHeight: 0, // 胶囊高度
    userInfo: {
      isLogin: false,
      avatar: 'https://chc-1326568539.cos.ap-nanjing.myqcloud.com/default_avatar.png'
    },
    showOfficial: false,// 是否展示official-account组件，特定场景值下展示
    channel: 1,
    city: '',
  },

  /**
   * 获取导航栏相关参数
   */
  setNavBarInfo() {
    const systemInfo = wx.getSystemInfoSync();
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏到胶囊的间距（胶囊距上距离 - 状态栏高度） * 2 + 胶囊高度 + 状态栏高度
    this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
    this.globalData.menuBottom = menuButtonInfo.top - systemInfo.statusBarHeight;
    this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    this.globalData.menuHeight = menuButtonInfo.height;
  }
})